package com.qth.valet.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.qth.valet.common.utils.ResponseResult;
import com.qth.valet.common.utils.StatusConstants;
import com.qth.valet.entity.vo.InsuranceSucVo;
import com.qth.valet.entity.vo.InsuranceVo;
import com.qth.valet.service.InsuranceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * <p>
 * 保险 服务实现类
 * </p>
 *
 * @author 蔡徐坤
 * @since 2025-08-26
 */
@Slf4j
@Service
public class InsuranceServiceImpl implements InsuranceService {

    @Value("${insurance.url}")
    private String insuranceUrl;

    @Value("${insurance.company}")
    private String insuranceCompany;

    @Resource
    private RestTemplate restTemplate;

    @Override
    public Double getBalance() {
        String url = insuranceUrl + StatusConstants.INSURANCE_BALANCE;
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.set("company", insuranceCompany);

        String result = restTemplate.postForObject(url, jsonObject1, String.class);
        log.info("banlance::"+result);
        if (StrUtil.isNotBlank(result)) {
            JSONObject data = JSONUtil.parseObj(result).getJSONObject("data");
            if (data != null && !data.isEmpty()) {
                return data.getDouble("balance");
            }
        }

        return null;
    }

    @Override
    public ResponseResult orderUpOne(InsuranceVo insuranceVo) {
        String url = insuranceUrl + StatusConstants.INSURANCE_CREATE;
        String result = restTemplate.postForObject(url, insuranceVo, String.class);
        log.info("orderUpOne::"+result);
        if (StrUtil.isNotBlank(result)) {
            JSONObject data = JSONUtil.parseObj(result);
            if (!data.isEmpty()) {
                int code = data.getInt("code");
                String msg = data.getStr("msg");
                Integer ids = null;
                if (code == 200) {
                    JSONObject id = data.getJSONObject("data");
                    if (!id.isEmpty()) {
                        ids = id.getInt("id");
                    }
                }
                return ResponseResult.success(code, msg, ids);
            }
        }

        return ResponseResult.fail("生成保险失败。");
    }

    @Override
    public ResponseResult search(String order_id) {
        String url = insuranceUrl + StatusConstants.INSURANCE_QUERY_STS;
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.set("company", insuranceCompany);
        jsonObject1.set("order_id", order_id);

        String result = restTemplate.postForObject(url, jsonObject1, String.class);
        log.info("search::"+result);
        if (StrUtil.isNotBlank(result)) {
            JSONObject data = JSONUtil.parseObj(result);
            return ResponseResult.success(data.getInt("code"), data.getStr("msg"), data.getObj("data"));
        }

        return ResponseResult.fail("查不到数据");
    }

    @Override
    public ResponseResult orderSuc(InsuranceSucVo insuranceSucVo) {
        String url = insuranceUrl + StatusConstants.INSURANCE_FINISH;

        String result = restTemplate.postForObject(url, insuranceSucVo, String.class);
        log.info("orderSuc::"+result);
        if (StrUtil.isNotBlank(result)) {
            JSONObject data = JSONUtil.parseObj(result);
            return ResponseResult.success(data.getInt("code"), data.getStr("msg"));
        }

        return ResponseResult.fail("保险订单完成失败。");
    }
}
